﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMKB_Repository;
using System.Net;

namespace PMKB_Web.Controllers
{
    public partial class TagController : Controller
    {
        //
        // GET: /Tag/

        public virtual ActionResult Index(string id,int? pageNumber, int? pageSize)
        {
            if (string.IsNullOrEmpty(id))
            {
                return RedirectToAction(MVC.Tag.List(pageNumber, pageSize));
            }
            clsLoadTags c = new clsLoadTags();
            c.ConnectionName = SiteUtils.ConnectionString;
            long idTag;
            if (!long.TryParse(id, out idTag))
            {
                
                var t = c.tagsFromName(id);
                if (t.Count == 0)
                    return Content("does not find " + id);//TODO: make view
                if(t.Count >1)
                    return Content("too much tags for " + id);//TODO: make view

                return RedirectToAction(MVC.Tag.Index(t[0].TagID.ToString(),pageNumber,pageSize));
            }
            var data = c.LoadTag(idTag, pageNumber??1,pageSize??10);
            return View(data);


        }
        public virtual JsonResult DeleteTag(int id)
        {
            try
            {

                clsLoadTags c = new clsLoadTags();
                c.ConnectionName = SiteUtils.ConnectionString;
                c.DeleteTag(id);
                return Json(new { ok = true, message = "" });

            }
            catch (Exception ex)
            {
                //TODO: log
                var errormsg = string.Format(Resources.Resources.ErrorDel, ex.Message);
                return Json(new { ok = false, message = errormsg });
            }
        }
        public virtual ActionResult List(int? pageNumber, int? pageSize)
        {
            clsLoadTags c = new clsLoadTags();
            c.ConnectionName = SiteUtils.ConnectionString;
            var data = c.LoadTags(pageNumber ?? 1, pageSize ?? 10);
            return View(data);


        }
        [HttpPost]
        public ActionResult SaveData(IEnumerable<KVP.KVPNew<long, string>> list)
        {
            int i=0;
            foreach (var item in list)
            {
                if (string.IsNullOrEmpty(item.Value))
                {
                    ModelState.AddModelError(i.ToString(),string.Format("tag {0} is empty",i));
                    i++;
                }
            }
            if (!ModelState.IsValid)
            {

                return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
            }
            else {
                clsLoadTags c = new clsLoadTags();
                c.ConnectionName = SiteUtils.ConnectionString;
                c.UpdateTags(list.Where(x=>x.Modified));
            }
            return RedirectToAction("List"); 
        }
        [HttpPost]
        public virtual JsonResult AddNewTag(string id)
        {
             
            try
            {
                clsLoadTags c = new clsLoadTags();
                c.ConnectionName = SiteUtils.ConnectionString;
                var item=c.AddNewTag(id);
                return Json(new { ok = true, message = "" ,data=item});

            }
            catch (Exception ex)
            {
                //TODO: log
                var errormsg = string.Format(Resources.Resources.ErrorAdd, ex.Message);
                return Json(new { ok = false, message = errormsg });
            }

       
        }
        [HttpPost]
        public virtual JsonResult RenameTag(int id, string name)
        {
            try
            {
                clsLoadTags c = new clsLoadTags();
                c.ConnectionName = SiteUtils.ConnectionString;
                var item=c.RenameTags(id, name);
                return Json(new { ok = true, message = ""});

            }
            catch (Exception ex)
            {
                //TODO: log
                var errormsg = string.Format(Resources.Resources.ErrorRename, ex.Message);
                return Json(new { ok = false, errormsg });
            }
        }

    }
}
